package com.eunion.web.cms.entity.spec;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.eunion.web.cms.entity.StudentVolunteer;

public final class StudentVolunteerSpec {

    public static Specification<StudentVolunteer> build(StudentVolunteer queryBean) {
        return new StudentVolunteerQuery(queryBean);
    }

    private static class StudentVolunteerQuery implements Specification<StudentVolunteer> {

        private StudentVolunteer queryBean;

        private StudentVolunteerQuery(StudentVolunteer queryBean) {
            this.queryBean = queryBean;
        }

        @Override
        public Predicate toPredicate(Root<StudentVolunteer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> list = getPredicates(root, query, cb, queryBean);
            Predicate[] preList = new Predicate[list.size()];
            return cb.and(list.toArray(preList));
        }

    }

    private static List<Predicate> getPredicates(Root<StudentVolunteer> root, CriteriaQuery<?> query, CriteriaBuilder cb,
        StudentVolunteer queryBean) {
        // 条件集合
        List<Predicate> list = new ArrayList<>();

        // 手机号条件
        String phone = queryBean.getPhone();
        if (StringUtils.isNotBlank(phone)) {
            list.add(cb.equal(root.get("phone"), phone.trim()));
        }

        // 考生昵称条件
        String nickname = queryBean.getNickname();
        if (StringUtils.isNotBlank(nickname)) {
            list.add(cb.like(root.get("nickname"), "%" + nickname.trim() + "%"));
        }

        // 性别条件
        String gender = queryBean.getGender();
        if (StringUtils.isNotBlank(gender)) {
            list.add(cb.equal(root.get("gender"), gender.trim()));
        }

        // 省份条件
        String provinceName = queryBean.getProvinceName();
        if (StringUtils.isNotBlank(provinceName)) {
            list.add(cb.equal(root.get("provinceName"), provinceName.trim()));
        }

        // 学科方向条件
        Integer direction = queryBean.getDirection();
        if (direction != null && direction > 0) {
            list.add(cb.equal(root.get("direction"), direction));
        }

        // 分数条件
        int score = queryBean.getScore();
        if (score > 0) {
            list.add(cb.equal(root.get("score"), score));
        }

        // 学校代码条件
        String collegeCode = queryBean.getCollegeCode();
        if (StringUtils.isNotBlank(collegeCode)) {
            list.add(cb.equal(root.get("collegeCode"), collegeCode.trim()));
        }

        // 学校名称条件
        String collegeName = queryBean.getCollegeName();
        if (StringUtils.isNotBlank(collegeName)) {
            list.add(cb.like(root.get("collegeName"), "%" + collegeName.trim() + "%"));
        }

        // 专业代码条件
        String majorCode = queryBean.getMajorCode();
        if (StringUtils.isNotBlank(majorCode)) {
            list.add(cb.equal(root.get("majorCode"), majorCode.trim()));
        }

        // 专业名称条件
        String majorName = queryBean.getMajorName();
        if (StringUtils.isNotBlank(majorName)) {
            list.add(cb.like(root.get("majorName"), "%" + majorName.trim() + "%"));
        }

        return list;
    }
}
